/*
 * validateCreateAccount.js
 * v0.03
 * 3/23/2011
 *
 * Form validation for creating account.
 */
	
	//This function checks validity of information for creating an account.
	function validateCreateAccount(){
		//Initialize variables.
		var flag = 0;
		var username 	=  document.getElementById('username');
		var password1 	=  document.getElementById('password1');
		var password2	=  document.getElementById('password2');
		var name 		=  document.getElementById('name');
		var title	 	=  document.getElementById('title');
		var position 	=  document.getElementById('position');
		
		if(checkUsername(username)) {printError("username",1);flag++;}
		else {printError("username",-1);}
		
		if(checkPassword(password1)){printError("password1",1);flag++;}
		else {printError("password1",-1);}
		
		if(checkPassword(password2) || (password1.value != password2.value))
			{printError("password2",1);flag++;}
		else {printError("password2",-1);}
		
		if(checkName(name)) {printError("name",1);flag++;}
		else {printError("name",-1);}
		
		if(checkName(title)) {printError("title",1);flag++;}
		else {printError("title",-1);}
		
		if(checkName(position)) {printError("position",1);flag++;}
		else {printError("position",-1);}
		
		//If all inputs are valid
		if(flag == 0){
			return true;
		}	
		//Else
		return false;
	}
	
	//This function checks format of username.
	function checkUsername(elem){
		//Initialize variable with regex pattern for alpha numeric characters.
		var ck_username = /^[A-Za-z0-9_]{6,30}$/;
		
		if (ck_username.test(elem.value)) return false;
		
		return true;	
	}
	
	//This function checks format of name.
	function checkName(elem){
		//Initialize variable with regex pattern for characters.
		var ck_name = /^[A-Za-z0-9,. ]{3,20}$/;
		
		if(ck_name.test(elem.value)) return false;
		
		return true;
	}
	
	//This function checks format of password.
	function checkPassword(elem){
		//Initialize variable with regex pattern for alpha numeric characters only.
		var ck_password =  /^[A-Za-z0-9!@#$%^&*()_]{1,50}$/;
		
		if (ck_password.test(elem.value)) return false;
			return true;	
	}
	
	//This function prints out error messages.
	function printError(elem,x){		
		var str = "";
		
		switch(x){
			case 0: str = "This field is required."; break;
			case 1: str = "Invalid " + elem + "."; break;
			default: str="";
		}	
		document.getElementById(elem+"error").innerHTML = str;		
	}